package com.zhuiyun.project.api.system.businesPost.controller;

import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.system.businesPost.entity.SysPost;
import com.zhuiyun.project.api.system.businesPost.service.SysBusinesPostService;
import com.zhuiyun.project.common.controller.BaseController;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.constants.CommonConstants;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName SysBusinesPost
 * @Description TODO 租户岗位管理
 * @Author wx
 * @Date 2023/12/6 15:39
 **/
@RestController
@Api(tags = "租户岗位管理")
@Slf4j
@CrossOrigin
@RequestMapping("/SysBusinesPost")
public class SysBusinesPostController extends BaseController {
    @Autowired
    private SysBusinesPostService sysBusinesPostService;

    /**
    * @Author wx
    * @Description 获取岗位列表
    * @Date 2023/12/6 15:47
    * @Return
    * @param post
    */
    @ApiOperation(value = "获取岗位列表")
    @OptLog(modular = "岗位管理", type = CommonConstants.SELECT, describe = "获取岗位列表")
    @GetMapping("/list")
    public CommonResult list(SysPost post, PageRequest pageRequest)
    {
        return sysBusinesPostService.selectPostList(post,pageRequest);
    }

    /**
    * @Author wx
    * @Description 新增岗位
    * @Date 2023/12/6 16:01
    * @Return
    * @param post
    */
    @ApiOperation(value = "新增岗位")
    @OptLog(modular = "岗位管理", type = CommonConstants.ADD, describe = "新增岗位")
    @PostMapping("/addPost")
    public CommonResult add(@Validated @RequestBody SysPost post)
    {
        if (!sysBusinesPostService.checkPostNameUnique(post))
        {
            return CommonResult.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (!sysBusinesPostService.checkPostCodeUnique(post))
        {
            return CommonResult.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(getUsername());
        return CommonResult.ok(sysBusinesPostService.insertPost(post));
    }

    /**
    * @Author wx
    * @Description 根据岗位编号获取详细信息
    * @Date 2023/12/6 16:15
    * @Return
    * @param post
    */
    @ApiOperation(value = "根据岗位编号获取详细信息")
    @OptLog(modular = "岗位管理", type = CommonConstants.SELECT, describe = "根据岗位编号获取详细信息")
    @PutMapping("/post")
    public CommonResult getInfo(@RequestBody SysPost post)
    {
        return CommonResult.ok(sysBusinesPostService.selectPostById(post.getPostId(),post.getVenueUniqueIdentifier()));
    }

    /**
    * @Author wx
    * @Description 修改岗位
    * @Date 2023/12/6 16:22
    * @Return
    * @param post
    */
    @ApiOperation(value = "修改岗位")
    @OptLog(modular = "岗位管理", type = CommonConstants.UPDATE, describe = "修改岗位")
    @PutMapping("/updatePost")
    public CommonResult edit(@Validated @RequestBody SysPost post)
    {
        if (!sysBusinesPostService.checkPostNameUnique(post))
        {
            return CommonResult.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (!sysBusinesPostService.checkPostCodeUnique(post))
        {
            return CommonResult.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(getUsername());
        return CommonResult.ok(sysBusinesPostService.updatePost(post));
    }

    /**
    * @Author wx
    * @Description 删除岗位
    * @Date 2023/12/6 16:27
    * @Return
    * @param postIds
    */
    @ApiOperation(value = "删除岗位")
    @OptLog(modular = "岗位管理", type = CommonConstants.DELETE, describe = "删除岗位")
    @DeleteMapping("/delPost")
    public CommonResult remove(@RequestParam("postIds") Long[] postIds, @RequestParam("venueUniqueIdentifier") String venueUniqueIdentifier) {
        System.out.println(postIds);
        System.out.println(venueUniqueIdentifier);
        return CommonResult.ok(sysBusinesPostService.deletePostByIds(postIds,venueUniqueIdentifier));
    }
}
